import 'dart:io';
import 'package:get/get.dart';
import 'package:oramemo/common/utils/dialog_util.dart';
import 'package:oramemo/core/models/api_response.dart';
import '../repositories/profile_repository.dart';
import '../models/user.dart';

class ProfileController extends GetxController {
  final ProfileRepository _repository = ProfileRepository();
  final _isLoading = false.obs;
  final Rx<User?> _user = Rx<User?>(null);

  bool get isLoading => _isLoading.value;

  User? get user => _user.value;

  Future<ApiResponse?> uploadAvatar(File file) async {
    try {
      _isLoading.value = true;
      final response = await _repository.uploadAvatar(file);
      if (response.isSuccess) {
        TipUtil.showSuccess('上传成功');
      } else {
        TipUtil.showError(response.msg);
      }
      return response;
    } catch (e) {
      TipUtil.showError('上传失败：$e');
    } finally {
      _isLoading.value = false;
    }
    return null;
  }

  Future<ApiResponse?> updateProfile({
    String? nickname,
    String? email,
    String? phoneNumber,
  }) async {
    try {
      _isLoading.value = true;
      final response = await _repository.updateProfile(
        nickname: nickname,
        email: email,
        phoneNumber: phoneNumber,
      );
      if (response.isSuccess) {
        // Get.snackbar('Success', 'Profile updated successfully');
      } else {
        Get.snackbar('Error', response.msg);
      }
      return response;
    } catch (e) {
      Get.snackbar('Error', e.toString());
    } finally {
      _isLoading.value = false;
    }
    return null;
  }
}
